package cn.itcast.mq.spring;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAMQPSend {

   /* @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void sendMessage(){
        String queueName = "simple.queue";
        String message = "fuck spring amqp !";
        //rabbitTemplate.convertAndSend(queue,messages);
        rabbitTemplate.convertAndSend(queueName, message);
    }*/
   @Autowired
   private RabbitTemplate rabbitTemplate;

    /**
     * 直接向队列发送消息
     */
    @Test
    public void testSimpleQueue() {
        // 队列名称
        String queueName = "simple.queue";
        // 消息
        String message = "fuck, spring amqp!";
        // 发送消息
        rabbitTemplate.convertAndSend(queueName, message);
    }

    @Test
    public void sendMany() throws InterruptedException {
        String queueName = "simple.queue";
        String msg="hello spring_";

        for (int i = 0; i < 50; i++) {
            rabbitTemplate.convertAndSend(queueName,msg+i);
            Thread.sleep(20);
        }
    }


    /**
     * 向FanoutExchange交换机发送消息
     */
    @Test
    public void sendToFanout(){
        //交换机名
        String exchangeName = "zhang.fanout";

        //要发送的消息
        String msg = "hello Zhang";
        rabbitTemplate.convertAndSend(exchangeName, "", msg);
    }


    /*
    根据routingKey，向DirectExchange交换机发送信息，
     */
    @Test
    public void sendToDirect(){
        String exchangeName = "lin.direct";
        String msg = "Mr. Zhang is handsome boy";
        rabbitTemplate.convertAndSend(exchangeName, "green", msg);

    }




}
